// pages/message/message.js
const appdata = getApp()
const AV = getApp().globalData.AV
Page({
  data: {
    array: ["保密", '男', '女'], // 性别
    index: 0, // 性别下表
    date: '请选择生日', //生日
    region: ['广东省', '广州市', '海珠区'], // 地区
    name: "", // 姓名
    openid: "", //user唯一标识
    id: "", // 用户id
    cover: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0', //头像
  },
  inputChange(e){ // 填写姓名修改
    if (this.data.openid !== null) {
      const todo = AV.Object.createWithoutData('users', this.data.id);
      todo.set('username', e.detail.value);
      todo.save().then((todo) => {
        wx.showToast({
          title: "修改成功"
        })
      });
    }
  },
  upload() { // 上传头像
    var that = this
    wx.chooseMedia({
      count: 1,
      mediaType: ['image', 'video'],
      sourceType: ['album', 'camera'],
      success(res) {
        var localFileUri = res.tempFiles[0].tempFilePath
        var fileName = res.tempFiles[0].tempFilePath.split('/')[3]
        const data = { blob: { uri: localFileUri } }
        const file = new AV.File(fileName, data)
        file.save().then((file) => {
          console.log(`文件保存完成。URL：${file.url()}`);
          that.setData({
            cover: file.url()
          })
          if (that.data.openid !== "") {
            const todo = AV.Object.createWithoutData('users', that.data.id);
            todo.set('userimg', that.data.cover);
            todo.save().then((todo) => {
              wx.showToast({
                title: "修改成功"
              })
            });
          }
        });
      }
    })
  },
  bindPickerChange: function (e) { // 性别 
    this.setData({
      index: e.detail.value
    })
    if (this.data.openid !== "") {
      const todo = AV.Object.createWithoutData('users', this.data.id);
      todo.set('sex', this.data.index);
      todo.save().then((todo) => {
        wx.showToast({
          title: "修改成功"
        })
      });
    }

  },
  bindDateChange: function (e) { //生日
    this.setData({
      date: e.detail.value
    })
    if (this.data.openid !== "") {
      const todo = AV.Object.createWithoutData('users', this.data.id);
      todo.set('birthday', this.data.date);
      todo.save().then((todo) => {
        wx.showToast({
          title: "修改成功"
        })
      });
    }
  },
  bindRegionChange: function (e) { // 地区
    this.setData({
      region: e.detail.value
    })
    const todo = AV.Object.createWithoutData('users', this.data.id);
    todo.set('region', this.data.region);
    todo.save().then((todo) => {
      wx.showToast({
        title: "修改成功"
      })
    });
  },
  add() { // 用户详情
    const query = new AV.Query('users');
    query.equalTo('openId', this.data.openid);
    query.find().then((students) => {
      this.setData({
        id: students[0].id,
        index:students[0].attributes.sex,
        region:students[0].attributes.region,
        name:students[0].attributes.username,
        cover:students[0].attributes.userimg,
        date:students[0].attributes.birthday,
      })
    })

  },
  onShow() {
    if (appdata.globalData.userInfo == null) {
        wx.showToast({
          title: '未登录',
          icon: 'error',
          duration: 2000
        })
      } else {
        const openids = appdata.globalData.userInfo
        this.setData({
          openid: openids
        })
        this.add()
      }
  },
})